//
//  MQTTCoverTool.m
//  MQTTNetworkTools
//
//  Created by Yahaong on 16/8/2.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "MQTTCoverTool.h"
#define APNS_STR_TOTAL_LEN  8
#define LUID_STR_TOTAL_LEN  16
#define OTHER_STR_TOTAL_LEN  2
#define LinkageNum_STR_TOTAL_LEN  2
@implementation MQTTCoverTool

// Byte转16进制字符串
+ (NSString *)hexStringFromByte:(Byte *)bytes length:(NSInteger)length
{
    NSMutableString *str=[[NSMutableString alloc]init];
    for (int i=0; i<length; i++) {
        Byte byte = bytes[i];
        NSString *value=[NSString stringWithFormat:@"%02x",byte];
        [str appendString:value];
    }
    return str;
}
+ (NSString*)apsnToString:(XAITYPEAPSN)apsn
{
    NSString* apns_end_str = [NSString stringWithFormat:@"%lx",(long)apsn];
    
    unichar ucdata[16] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    NSInteger  apns_start_len =   APNS_STR_TOTAL_LEN - [apns_end_str length];
    if (apns_end_str < 0 ) {
        return @"";
    }
    NSString* apns_start_str = [[NSString alloc] initWithCharacters:ucdata length:apns_start_len];
    NSString*  APNS_Str = [[NSString alloc] initWithFormat:@"0x%@%@",apns_start_str,apns_end_str];
    return APNS_Str;
}
+ (NSString*) luidToString:(XAITYPELUID)luid
{
    NSString* luid_end_str = [NSString stringWithFormat:@"%lx",(long)luid];
    unichar ucdata[16] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    NSInteger  luid_start_len =  LUID_STR_TOTAL_LEN - [luid_end_str length];
    if ( luid_end_str < 0) {
        return @"";
    }
    NSString* luid_start_str = [[NSString alloc] initWithCharacters:ucdata length:luid_start_len];
    NSString*  LUID_Str = [[NSString alloc] initWithFormat:@"0x%@%@",luid_start_str,luid_end_str];
    return LUID_Str;
}
+ (NSString*) mobileDevTableTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    return [MQTTCoverTool stringFormat:@"%@/MOBILES/%@/OUT/DEV" APNS:APNS luid:luid];
}

+ (NSString*) mobileCtrTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
    return [MQTTCoverTool stringFormat:@"%@/MOBILES/%@/IN" APNS:apsn luid:luid];
}
+ (NSString*) mobileConfigUserTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
    return [MQTTCoverTool stringFormat:@"%@/MOBILES/%@/CONFIG/USER/+/ACTIVE" APNS:apsn luid:luid];
}
+ (NSString*) mobileConfigAllTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
    return [MQTTCoverTool stringFormat:@"%@/MOBILES/%@/CONFIG/#" APNS:apsn luid:luid];
}
+ (NSString*) mobileStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid other:(uint8_t)other{
    //0x%08x/MOBILES/0x%016llx/OUT/STATUS/%02d
    NSString*  other_Str = [MQTTCoverTool stringOther:other];
    return [NSString stringWithFormat:@"%@/%@"
            ,[MQTTCoverTool stringFormat:@"%@/MOBILES/%@/OUT/STATUS" APNS:APNS luid:luid]
            ,other_Str];
}
+ (NSString*) mobileAllDevStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid
{
    return [NSString stringWithFormat:@"%@/MOBILES/%@/OUT/STATUS/+",[MQTTCoverTool apsnToString:APNS],[MQTTCoverTool luidToString:luid]];
}
+ (NSString*) stringFormat:( NSString*)format APNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    NSString*  APNS_Str  = [MQTTCoverTool apsnToString:APNS];
    NSString *LUID_Str = [MQTTCoverTool luidToString:luid];
    __autoreleasing NSString* ret = [[NSString  alloc] initWithFormat:format,APNS_Str,LUID_Str];
    return  ret;
}

+ (NSString*) stringFormat:( NSString*)format type:(NSString*)type APNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    NSString*  APNS_Str  = [MQTTCoverTool apsnToString:APNS];
    NSString *LUID_Str = [MQTTCoverTool luidToString:luid];
    __autoreleasing NSString* ret = [[NSString  alloc] initWithFormat:format,APNS_Str,type, LUID_Str];
    return  ret;
}

+ (NSString*) gatewayCtrlTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    //0x%08x/GATEWAY/0x%016llx/IN
    return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/IN" APNS:APNS luid:luid];
}
+ (NSString*) gatewayConfigLinkageTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid {
    return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/CONFIG/LINKACTION/+" APNS:APNS luid:luid];
}

+ (NSString*) gatewayAllStatusTableTopicWithAPNS:(XAITYPEAPSN)APNS
{
    return [NSString stringWithFormat:@"%@/GATEWAY/0x0000000000000001/OUT/STATUS/+",[MQTTCoverTool apsnToString:APNS]];
}
+ (NSString*) gatwayStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid other:(uint8_t)other
{
    NSString*  other_Str = [[NSString alloc] initWithString:[MQTTCoverTool stringOther:other]];
    NSString*  part_str = [[NSString alloc] initWithString:[MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/OUT/STATUS" APNS:APNS luid:luid]];
    NSString*  formart = @"%@/%@";
    __autoreleasing NSString* ret = [[NSString alloc] initWithFormat:formart,part_str ,other_Str];
    return ret;

}
+ (NSString*) gatewayConfigAllUserTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
     return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/CONFIG/USER/#" APNS:apsn luid:luid];
}

+ (NSString*) gatewayConfigAllUserActiveTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
    return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/CONFIG/USER/+/ACTIVE" APNS:apsn luid:luid];
}

+ (NSString*) gatewayConfigUserPushTopicWithAPNS:(XAITYPEAPSN)apsn luid:(XAITYPELUID)luid
{
    return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/CONFIG/USER/+/PUSH" APNS:apsn luid:luid];
}

+ (NSString*) gatewayDevTableTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    return [MQTTCoverTool stringFormat:@"%@/GATEWAY/%@/OUT/DEV" APNS:APNS luid:luid];
}

+ (NSString*) stringOther:(uint8_t)other
{
    NSString* other_end_str = [[NSString alloc] initWithFormat:@"%x",other];
    unichar ucdata[16] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    ssize_t  other_start_len =   OTHER_STR_TOTAL_LEN - [other_end_str length];
    if (other_start_len < 0) {
        return @"";
    }
    NSString* other_start_str = [[NSString alloc] initWithCharacters:ucdata length:other_start_len];
    __autoreleasing NSString*  Other_Str = [[NSString alloc] initWithFormat:@"0x%@%@",other_start_str,other_end_str];
    return Other_Str;
}

+ (NSString*) stringLinkageNum:(uint8_t)num
{
    NSString* num_end_str = [[NSString alloc] initWithFormat:@"%x",num];
    unichar ucdata[16] = {'0','0'};
    ssize_t  num_start_len =   LinkageNum_STR_TOTAL_LEN - [num_end_str length];
    if (num_start_len < 0)
    {
        return @"";
    }
    NSString* num_start_str = [[NSString alloc] initWithCharacters:ucdata length:num_start_len];
    NSString*  num_Str = [[NSString alloc] initWithFormat:@"0x%@%@",num_start_str,num_end_str];
    return num_Str;
}

+ (NSString*) nodeAllDevTableTopicWithAPNS:(XAITYPEAPSN)APNS
{
    return [NSString stringWithFormat:@"%@/NODES/+/OUT/DEV",[MQTTCoverTool apsnToString:APNS]];
}
+ (NSString*) nodeAllDevStatusTopicWithAPNS:(XAITYPEAPSN)APNS
{
     return [NSString stringWithFormat:@"%@/NODES/+/OUT/STATUS/+",[MQTTCoverTool apsnToString:APNS]];
}
+ (NSString*) nodeDevStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid
{
    return [NSString stringWithFormat:@"%@/NODES/%@/OUT/STATUS/+",[MQTTCoverTool apsnToString:APNS],[MQTTCoverTool luidToString:luid]];
}
+ (NSString*) nodeStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid other:(uint8_t)other{
    //0x%08x/NODES/0x%016llx/OUT/STATUS/%02d
    NSString*  other_Str = [[NSString alloc] initWithString:[MQTTCoverTool stringOther:other]];
    NSString*  part_str = [[NSString alloc] initWithString:[MQTTCoverTool stringFormat:@"%@/NODES/%@/OUT/STATUS" APNS:APNS luid:luid]];
    NSString*  formart = @"%@/%@";
    __autoreleasing NSString* ret = [[NSString alloc] initWithFormat:formart,part_str ,other_Str];
    return ret;
}

+ (NSString*) nodeDevTableTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid{
    return [MQTTCoverTool stringFormat:@"%@/NODES/%@/OUT/DEV" APNS:APNS luid:luid];
}

+ (NSString*) nodeCtrlTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid
{
    //0x%08x/NODES/0x%016llx/IN
    return [MQTTCoverTool stringFormat:@"%@/NODES/%@/IN" APNS:APNS luid:luid];
}

+ (NSString*) virtualStatusTopicWithAPNS:(XAITYPEAPSN)APNS luid:(XAITYPELUID)luid
{
    return [NSString stringWithFormat:@"%@/VIRTUALS/%@/OUT/STATUS/+",[MQTTCoverTool apsnToString:APNS],[MQTTCoverTool luidToString:luid]];
}
+ (uint64_t)decimalWithHexString:(NSString *)hexString
{
    return  strtoull([hexString UTF8String], 0, 16);
}

+ (XAITYPEAPSN)dataToApsn:(void *)data
{
    XAITYPEAPSN apsn = 0;
    memcpy(&apsn, data, 4);
    return apsn;
}
+ (XAITYPELUID)dataToLuid:(void *)data
{
    XAITYPELUID luid = 0;
    memcpy(&luid, data + 4, 8);
    return luid;
}

+ (NSData*)hexStringToByte:(NSString*)string
{
    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([hexString length]%2!=0) {
        NSLog(@"长度不对哦");
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}
+ (NSString *)phoneNumFromLuid:(XAITYPELUID)aLuid
{
    XAITYPELUID luid = aLuid;
    luid = luid >> 24;
    NSString *phoneNum = [NSString stringWithFormat:@"%zd",luid];
    return phoneNum;
}

+ (XAITYPELUID)mobileToLuid:(NSString *)mobile
{
    XAITYPELUID value =  [MQTTCoverTool decimalWithHexString:mobile];
    value |= 0x0300000000000000L;
    NSString* luidStr = [NSString stringWithFormat:@"%016llx",value];
    XAITYPELUID newLuid = [MQTTCoverTool decimalWithHexString:luidStr];
    return newLuid;
}

+ (NSString *)luidToMobile:(XAITYPELUID)luid
{
    XAITYPELUID newLuid = luid;
    newLuid &=  0x00ffffffffffffffL;
    NSString *strMobile = [NSString stringWithFormat:@"%llx",newLuid];
    return strMobile;
}

+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    if (mobileNum.length != 11) {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,152,155,156,170,171,176,185,186
     * 电信号段: 133,134,153,170,177,180,181,189
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,145,152,155,156,170,171,176,185,186
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[256]|7[016]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,134,153,170,173,177,180,181,189
     */
    NSString *CT = @"^1(3[34]|53|7[037]|8[019])\\d{8}$";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}



@end
